package service

import (
	"fmt"
	"testhttp/dao"
	"testhttp/model"
	"time"

	"github.com/kataras/iris/v12"
)

func ListTemp(data model.HttpParamsOrderTemp) iris.Map {
	queryparam := " is_deleted = 0 "
	a := dao.ListTemp(fmt.Sprint(queryparam), data.Pagesize, data.Page)
	res := make([]model.OrderTempList, len(a))
	for x := 0; x < len(a); x++ {
		res[x].TempName = a[x].TempName
		res[x].TempUid = a[x].TempUid
		dealer, _ := GetDealerbyId(a[x].DealerId)
		if len(dealer) == 0 {
			res[x].Dealer = "Null"

			// log.Print("Debug get dealer（err != nil）:", res[x].TempName, dealer, err)
		} else {
			res[x].Dealer = dealer[0].Dealer
			// log.Print("Debug get dealer:", dealer, err)
		}

	}
	respones := iris.Map{
		"code":  0,
		"msg":   "",
		"count": len(res),
		"data":  res,
	}
	return respones
}
func TempListforSelect() iris.Map {
	queryparam := " is_deleted = 0 "
	a := dao.TempListforSelect(fmt.Sprint(queryparam))
	res := make([]model.OrderTempList, len(a))
	for x := 0; x < len(a); x++ {
		res[x].TempName = a[x].TempName
		res[x].TempUid = a[x].TempUid
		dealer, _ := GetDealerbyId(a[x].DealerId)
		if len(dealer) == 0 {
			res[x].Dealer = "Null"

			// log.Print("Debug get dealer(err != nil):", res[x].TempName, dealer, err)
		} else {
			res[x].Dealer = dealer[0].Dealer
			// log.Print("Debug get dealer:", dealer, err)
		}

	}
	respones := iris.Map{
		"code": 0,
		"msg":  "",
		"data": res,
	}
	return respones
}

func DelTemp(data []model.OrderTemp) {
	dao.DelTemp(data)
}

func GetOrderTempByUid(uid string) []string {
	data := dao.GetTemp(uid)
	res := make([]string, 0)
	for x := 0; x < len(data); x++ {
		res = append(res, data[x].ColValue)
	}
	// log.Print("Debug get temp:", res)

	return res
}

func AddTemp(ordertemp []model.OrderTemp) {
	now := time.Now()

	//封装创建时间，更新时间到数据集合ordertemp
	for x := 0; x < len(ordertemp); x++ {
		ordertemp[x].CreateTime = now
		ordertemp[x].UpdateTime = now
		ordertemp[x].IsDeleted = 0
	}

	dao.AddTemp(ordertemp)
}
